package com.yesun.task.pipeline.config;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class DBContextHolder {

    //通过ThreadLocal保存当前的状态，是的同一个线程中可以在任意地方获取到这个状态
    private static ThreadLocal<String> contextHolder = new ThreadLocal<>();
    private static final AtomicInteger counter = new AtomicInteger(-1);

    //设置
    public static void setDbType(DBTypeEnum dbType) {
        log.info("切换"+ dbType.toString() +"数据源");
        contextHolder.set(dbType.toString());
    }

    //读取
    public static String getDbType() {
        return contextHolder.get();
    }

    //切换到mysql
    public static void mysql() {
        setDbType(DBTypeEnum.MYSQL);
    }

    //切换到clickhouse
    public static void clickhouse() {
        setDbType(DBTypeEnum.CLICKHOUSE);
    }

    public static void clearDBType() {
        contextHolder.remove();
    }

}
